﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace BTS6000
{
    static class Program
    {
        #region SHA1

        static public string Compute(string FileName)
        {
            byte[] Hash = null;

            using (System.IO.FileStream fs = System.IO.File.OpenRead(FileName))
            {
                System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
                Hash = sha.ComputeHash(fs);
                fs.Close();
            }
            if (Hash == null)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder sb = new StringBuilder(48);
                for (int i = 0; i < Hash.Length; i++)
                {
                    sb.AppendFormat("{0:X2}", Hash[i]);
                }
                return sb.ToString();
            }
        }

        static public bool Verify(string filename, string hash)
        {
            System.IO.FileInfo fInfo = new System.IO.FileInfo(filename);
            if (fInfo.Exists)
            {
                if (string.Compare(Compute(fInfo.FullName), hash, true) == 0)
                {
                    return true;
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show(string.Format("{0}\n{1}", "Invalid file!", filename), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(string.Format("{0}\n{1}", "File not found!", filename), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return false;
            }
        }

        static private void Delete(string filename)
        {
            try
            {
                System.IO.FileInfo fInfo = new System.IO.FileInfo(filename);
                if (fInfo.Exists)
                {
                    fInfo.Delete();
                }
            }
            catch
            {
            }
        }

        #endregion

        static internal frmMain MDI;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            string exe = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location);
            Kuge.AppSetting.INI.Password = "abcdefghijklmnopqrstuvwxyz";

            switch(Kuge.AppSetting.INI.GetString("Default","Language"))
            {
                case "CHS":
                    Language.Select(Language.LCID.CHS);
                    break;
                case "CHT":
                    Language.Select(Language.LCID.CHT);
                    break;
                default:
                    Language.Select(Language.LCID.ENG);
                    break;
            }

#if (!DEBUG) && (true)
            Delete("BTS+.pdb");
            Delete("Parameter.pdb");
            Delete("Testpad.BTS.pdb");
            Delete("BTS+.vshost.exe");
            Delete("BTS+.vshost.exe.manifest");
            if (!Verify(Path.Combine(Application.StartupPath, "Message.dll"), "A5ED63CF9851F405F09BF239F33309377E719773"))
            {
                return;
            }
            if (!Verify(Path.Combine(Application.StartupPath, "AppSetting.dll"), "CE48B1F21C5BCA6949D0E7C3F9C77870C4FFC7AC"))
            {
                return;
            }
            if (!Verify(Path.Combine(Application.StartupPath, "Conversion.dll"), "2302B78F1FF5F28BDC01088B0B8F6D284240622D"))
            {
                return;
            }
            if (!Verify(Path.Combine(Application.StartupPath, "Parameter.dll"), "363B2B94B0EFD8136A86768C1E00510C8A4A5ED6"))
            {
                return;
            }
            if (!Verify(Path.Combine(Application.StartupPath, "Testpad.BTS.dll"), "682BDE41504D026A3B7B947549768E8900040CA7"))
            {
                return;
            }
#endif
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MDI = new frmMain();
            Application.Run(MDI);
        }
    }
}
